package bs.jizhang.controller;

import bs.jizhang.model.AmountType;
import bs.jizhang.repository.AmountTypeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/amount_type")
public class AmountTypeController {
    private final AmountTypeRepository Repository;

    @Autowired
    public AmountTypeController(AmountTypeRepository AmountTypeRepository) {
        this.Repository = AmountTypeRepository;
    }

    // 获取列表（支持分页和排序）
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> list(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(defaultValue = "id,asc") String sort) {
        Map<String, Object> response = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        // 解析排序参数
        String[] sortParams = sort.split(",");
        Sort.Direction direction = Sort.Direction.fromString(sortParams[1]);
        Sort sortBy = Sort.by(direction, sortParams[0]);

        // 创建分页对象
        Pageable pageable = PageRequest.of(page, size, sortBy);

        // 查询分页数据
        Page<AmountType> res = Repository.findAll(pageable);

        // 构建响应
        data.put("list", res.getContent());
        data.put("totalElements", res.getTotalElements());
        data.put("totalPages", res.getTotalPages());
        data.put("currentPage", res.getNumber());
        data.put("pageSize", res.getSize());
        data.put("hasNextPage", res.hasNext());

        response.put("code", 0);
        response.put("data", data);

        return ResponseEntity.ok(response);
    }

    // 获取详情
    @GetMapping("/get/{id}")
    public ResponseEntity<Map<String, Object>> get(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        AmountType res = Repository.findById(id).orElse(null);
        if (res != null) {
            response.put("status", 0);
            response.put("msg", "success");
            response.put("data", res);
        } else {
            response.put("status", 1);
            response.put("msg", "数据未找到");
        }
        return ResponseEntity.ok(response);
    }

    // 添加
    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> add(@RequestBody AmountType model) {
        Map<String, Object> response = new HashMap<>();
        AmountType res = Repository.save(model);
        response.put("status", 0);
        response.put("msg", "操作成功");
        response.put("data", res);
        return ResponseEntity.ok(response);
    }

    // 修改
    @PutMapping("/update/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody AmountType model) {
        Map<String, Object> response = new HashMap<>();
        if (Repository.existsById(id)) {
            model.setId(id);
            AmountType res = Repository.save(model);
            response.put("status", 0);
            response.put("msg", "操作成功");
            response.put("data", res);
        } else {
            response.put("status", 1);
            response.put("msg", "数据不存在");
        }
        return ResponseEntity.ok(response);
    }

    // 删除
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> response = new HashMap<>();
        if (Repository.existsById(id)) {
            Repository.deleteById(id);
            response.put("status", 0);
            response.put("msg", "操作成功");
        } else {
            response.put("status", 1);
            response.put("msg", "数据不存在");
        }
        return ResponseEntity.ok(response);
    }
}
